﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.DataAccess;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Data.DAO;

namespace Framework.Portal.Layouts.Admin.Components.Teenio.Category
{
    public partial class Permission : BaseControl
    {
        private readonly string MENUNAME = string.Empty;

        public Permission()
        {
        }

        public Permission(JSection section)
        {
            MENUNAME = section.title;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            lblUpdateStatus.Text = string.Empty;
            lblCommandName.Text = MENUNAME;
            if (!IsPostBack)
            {
                List<PortalUser> dtUsers = ManagerFactory.PortalUserManager.GetAll(Session.WorkingPortal().PORTALID);
                if (dtgUsers != null)
                {
                    dtgUsers.DataSource = dtUsers;
                    dtgUsers.DataBind();
                    List<TeenioCategory> dtMenu = ManagerFactory.TeenioCategoryManager.GetCategoriesByPortal(Session.WorkingPortal().PORTALID);
                    CmsAccess.LoadCategories(dtMenu, lstZones.Items);
                    rdoPermisions.SelectedIndex = -1;
                }
            }
        }

        protected void dtgUsers_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "roles")
            {
                rdoPermisions.SelectedIndex = -1;
                lstZones.SelectedIndex = -1;
                string[] param = e.CommandArgument.ToString().Split('/');
                txtUserID.Text = param[0];
                lblUsername.Text = param[1];
                TeenioPermission curPermision =
                    ManagerFactory.TeenioPermissionManager.GetPermision(Convert.ToInt32(param[0]), Session.WorkingPortal().PORTALID);
                if (curPermision != null)
                {
                    MiscUtility.SelectItemFromList(rdoPermisions, curPermision.PERMISION.ToString());
                    if (!string.IsNullOrEmpty(curPermision.ZONES))
                    {
                        foreach (ListItem item in lstZones.Items)
                        {
                            if (curPermision.ZONES.IndexOf("|" + item.Value + "|") > -1)
                                item.Selected = true;
                            else
                                item.Selected = false;
                        }
                    }
                }
            }
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            if (lblUsername.Text == string.Empty || txtUserID.Text == string.Empty) return;
            try
            {
                string permisionZoneCollection = "|";
                foreach (ListItem item in lstZones.Items)
                {
                    if (item.Selected) permisionZoneCollection += item.Value + "|";
                }
                if (permisionZoneCollection.Length == 1)
                    permisionZoneCollection = string.Empty;
                TeenioPermission info =
                    ManagerFactory.DAOEntity.GetInfo(new TeenioPermission()
                                                         {
                                                             USERID = Convert.ToInt32(txtUserID.Text),
                                                             PORTALID = Session.WorkingPortal().PORTALID
                                                         });
                if (info != null)
                {
                    ManagerFactory.DAOEntity.Update(new TeenioPermission
                                                        {
                                                            Key = "USERID",
                                                            USERID = Convert.ToInt32(txtUserID.Text),
                                                            PERMISION =
                                                                Convert.ToInt32(rdoPermisions.SelectedValue),
                                                            ZONES = permisionZoneCollection,
                                                            PORTALID = Session.WorkingPortal().PORTALID
                                                        });
                }
                else
                {
                    ManagerFactory.DAOEntity.Insert(new TeenioPermission
                    {
                        USERID = Convert.ToInt32(txtUserID.Text),
                        PERMISION = Convert.ToInt32(rdoPermisions.SelectedValue),
                        ZONES = permisionZoneCollection,
                        PORTALID = Session.WorkingPortal().PORTALID
                    });
                }
                lblUpdateStatus.Text = Localization.GetText("MESSAGE", "MSG_UPDATE_SUCCESS");
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = ex.Message;
            }
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            rdoPermisions.SelectedIndex = -1;
            lstZones.SelectedIndex = -1;
        }
    }
}